package de.hs.rm;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;

public class BHModeMouseActivity extends Activity implements OnTouchListener {
	
	private static final float MOVE_FAST = 1.50f;
	private static final float MOVE_NORMAL = 1f;
	private static final float MOVE_SLOW = 0.50f;
	
	private static final float SCROLL_FAST_THRESHOLD = 12f;
	private static final float SCROLL_SLOW_THRESHOLD = 4f;
	private static final int SCROLL_FAST = 3;
	private static final int SCROLL_SLOW = 1;
	
	private static final float VECTLEN_FAST = 300f;
	private static final float VECTLEN_NORMAL = 100f;
	//private static final float VECTLEN_SLOW = 50f;
	private static final float PRESSURE_THRESHOLD = 0.7f;
	private static final float EVENTTIME_THRESHOLD = 200f;
	
	private BHMainApplication mainApp= null;
	private LinearLayout linLayout = null;
	

	private float prevX = -1f;
	private float prevY = -1f;
	private float prevScroll = -1f;
	private float currX = -1f;
	private float currY = -1f;
	private float currScroll = -1;
	private float currVectLen = 0f;
	private int sendX = 0;
	private int sendY = 0;
	private int sendScroll = 0;
	
	private boolean isPressured_1 = false;
	private boolean isPressured_2 = false;
	private long prevEventTime_1 = -1;
	private long prevEventTime_2 = -1;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Drawable bgImage = getResources().getDrawable(R.drawable.mousebackground);
        bgImage.setAlpha(30);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.modemouse);
        
        mainApp = (BHMainApplication)this.getApplicationContext();
        
        linLayout = (LinearLayout)findViewById(R.id.mode_mouse_lin_layout);
        linLayout.setOnTouchListener(this);
    }

	@Override
	public boolean onTouch(View v, MotionEvent event) {
//		String names[] = { "DOWN" , "UP" , "MOVE" , "CANCEL" , "OUTSIDE" ,
//			      "POINTER_DOWN" , "POINTER_UP" , "7?" , "8?" , "9?" };
//		int action = event.getAction();
//	    int actionCode = action & MotionEvent.ACTION_MASK;
//		mainApp.sendBTMessage("pointer count: "
//				+ event.getPointerCount()
//				+ " event:ACTION_"
//				+ names[actionCode]
//		);
//		if (event.getAction() != event.ACTION_MOVE)
//		    {
//			    mainApp.sendBTMessage("index: "
//					+ pointerIndex
//					+ " pid: "
//					+ pointerId
//					+ " pressure: "
//					+ pressure
//			);
//		}
		int action = event.getActionMasked();
		int pointerIndex = event.getActionIndex();
	    int pointerId = event.getPointerId(pointerIndex);
	    float pressure = event.getPressure();
	    long eventTime = event.getEventTime();
	    float xPos = event.getX();
	    float yPos = event.getY();
	   
	    
	    if ((pointerIndex) == 0 && (pointerId == 0) && event.getPointerCount() == 1)
		{
	    	switch (event.getAction())
	    	{
	    		case MotionEvent.ACTION_DOWN:
	    			prevEventTime_1 = eventTime;
	    			if(pressure > PRESSURE_THRESHOLD)
	    			{
	    				isPressured_1 = true;
	    				mainApp.sendBTMessage("mouse:leftDown");
	    			}
	    			break;
	    		case MotionEvent.ACTION_UP:
	    			if(((eventTime - prevEventTime_1) < EVENTTIME_THRESHOLD)
	    					&& !isPressured_1)
	    			{
//	    				linLayout.setBackgroundColor(0x55A4C639);
//	    				linLayout.setBackgroundColor(0xffffffff);
//	    				linLayout.setBackgroundResource(R.drawable.mousebackground);
	    				mainApp.sendBTMessage("mouse:leftClick");
	    				prevEventTime_1 = -1;	    				
	    			}
	    			else if(isPressured_1)
	    			{
						isPressured_1 = false;
	    				mainApp.sendBTMessage("mouse:leftUp");
//	    				linLayout.setBackgroundColor(0xffffffff);
//	    				linLayout.setBackgroundResource(R.drawable.mousebackground);
	    			}
	    			prevX = -1f;
					prevY = -1f;
	    			break;
	    		case MotionEvent.ACTION_MOVE:
	    			evaluateMotion(xPos, yPos);
	    			break;
	    			
	    	}
		}
	    if((pointerIndex == 1) && (pointerId == 1) && (event.getPointerCount() == 2))
	    {
	    	switch (action)
	    	{
	    		case MotionEvent.ACTION_POINTER_DOWN:
	    			prevEventTime_2 = eventTime;
//	    			if(pressure > PRESSURE_THRESHOLD)
//	    			{
//	    				isPressured_2 = true;
//	    				mainApp.sendBTMessage("mouse:rightDown");
//	    			}
	    			break;
	    		case MotionEvent.ACTION_POINTER_UP:
	    			if(((eventTime - prevEventTime_2) < EVENTTIME_THRESHOLD)
	    					&& !isPressured_2)
	    			{
//	    				linLayout.setBackgroundColor(0x55880000);
//	    				linLayout.setBackgroundColor(0xffffffff);
//	    				linLayout.setBackgroundResource(R.drawable.mousebackground);
	    				mainApp.sendBTMessage("mouse:rightClick");
	    				
	    				
	    			}
//	    			else if(isPressured_2)
//	    			{
//						isPressured_2 = false;
//	    				mainApp.sendBTMessage("mouse:RightUp");
//	    				linLayout.setBackgroundColor(0xffffffff);
//	    				linLayout.setBackgroundResource(R.drawable.mousebackground);
//	    			}
	    			prevEventTime_2 = -1;
	    			prevX = -1f;
					prevY = -1f;
					prevScroll = -1f;
	    			break;
//	    		case MotionEvent.ACTION_MOVE:
//	    			evaluateMotion(xPos, yPos);
//	    			break;
	    			
	    	}    	
	    }
	    if((event.getPointerCount() == 2) 
	    		&& (event.getAction() == MotionEvent.ACTION_MOVE)
	    		&& ((eventTime - prevEventTime_2) > EVENTTIME_THRESHOLD)
	    		&& (pointerIndex == 0))
	    {
	    	if (!(prevScroll < 0f))
	    	{
	    		currScroll = yPos - prevScroll;
	    		prevScroll = (yPos + prevScroll)/2;
	    		
	    		if(Math.abs(currScroll) > SCROLL_FAST_THRESHOLD)
	    		{
	    			sendScroll = (int)(currScroll > 0? SCROLL_FAST :-SCROLL_FAST);
	    			mainApp.sendBTMessage("mouse:scrollyA"
	    					+ sendScroll
	    					+ "yE");
	    		}
	    		else if(Math.abs(currScroll) > SCROLL_SLOW_THRESHOLD)
	    		{
	    			sendScroll = (int)(currScroll > 0? SCROLL_SLOW:-SCROLL_SLOW);
	    			mainApp.sendBTMessage("mouse:scrollyA"
	    					+ sendScroll
	    					+ "yE");
	    		}
	    		
	    	}
	    	else
	    	{
	    		prevScroll = yPos;
	    	}
    		
    	}
	    return true;
	}
	
	private void evaluateMotion(float xPos, float yPos)
	{
		if (!(prevX < 0f) && !(prevY < 0f))
		{
			currX = xPos - prevX;
			currY = yPos - prevY;
			currVectLen = (currX * currX) + (currY * currY);
			prevX = (xPos + prevX)/2;
			prevY = (yPos + prevY)/2;
			sendX = (int)(currX * getMultiplier(currVectLen));
			sendY = (int)(currY * getMultiplier(currVectLen));
			mainApp.sendBTMessage("mouse:movexA"
					+ sendX
					+ "xEyA"
					+ sendY
					+ "yE");	
		}
		else
		{		
			prevX = xPos;
			prevY = yPos;
		}		
	}
	
	private float getMultiplier(float vectLen)
	{
		if (vectLen > VECTLEN_FAST)
			return MOVE_FAST;
		else if (vectLen > VECTLEN_NORMAL)
			return MOVE_NORMAL;
		return MOVE_SLOW;
	}
	
//	private void evaluatePointer_1(MotionEvent event)
//	{
//		switch(event.getAction()){
//			case MotionEvent.ACTION_MOVE:
//				evaluateMotion(new TouchCoords(
//						event.getX(),
//						event.getY(),
//						event.getEventTime()
//						));
//				break;
//			case MotionEvent.ACTION_UP:
//				prevX = -1f;
//				prevY = -1f;
//				break;
//		}
//	}
//	
//	private void evaluatePointer_2(MotionEvent event)
//	{
//		int action = event.getAction();
//	    int actionCode = action & MotionEvent.ACTION_MASK;
////	    if (event.getActionMasked() != MotionEvent.ACTION_MOVE)
////			mainApp.sendBTMessage("pointer count: "
////			+ event.getPointerCount()
////			+ " event:ACTION_"
////			+ actionCode
////			);
////		if (event.getAction() != MotionEvent.ACTION_MOVE)
////			mainApp.sendBTMessage("click");
//		switch(actionCode){
//			case MotionEvent.ACTION_POINTER_DOWN:
//				linLayout.setBackgroundColor(0x55A4C639);
//				mainApp.sendBTMessage("click");
//				break;
//			case MotionEvent.ACTION_POINTER_UP:
//				linLayout.setBackgroundColor(0xffffffff);
//				linLayout.setBackgroundResource(R.drawable.mousebackground);
//				mainApp.sendBTMessage("release");
//				break;
//		}
//	}
	
	
	
//	private class TouchCoords
//	{
//		float x;
//		float y;
//		long timeStamp;
//		
//		public TouchCoords(float x, float y, long timeStamp)
//		{
//			this.x = x;
//			this.y = y;
//			this.timeStamp = timeStamp;
//		}
//		
//		public float getX() {
//			return x;
//		}
//
//		public float getY() {
//			return y;
//		}
//
//		public long getTimeStamp() {
//			return timeStamp;
//		}
//	}
}